package de.servicezombie.tcmdb.rest.vo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;

import de.servicezombie.lang.ListUtils;
/**
 * <p>Utility class for tcmdb list results returned by a webservice.</p>
 * <b>Example:</b>
 * <pre>
 * items [
 *   // any instance of another Ws* class 
 * ]
 * </pre>
 * @author ChKa
 * @version n.a.
 * @see #items
 */
@XmlRootElement(name = "items")
@XmlAccessorType(XmlAccessType.FIELD)
public class WsListResult<T> implements Iterable<T>{

	/**
	 * list of known <code>Ws*<code> items contained in this result list
	 */
	@XmlElements({
			@XmlElement(name = "application", type = WsApplication.class),
			@XmlElement(name = "author", type = WsAuthor.class),
			@XmlElement(name = "criteria", type = WsQueryCriteria.class),
			@XmlElement(name = "severity", type = Severity.class),
			@XmlElement(name = "softwareBug", type = WsSoftwarebug.class),
			@XmlElement(name = "testcaseDefinition", type = WsTestcaseDefinition.class),
			@XmlElement(name = "testcaseStep", type = WsTestcaseStep.class),
			@XmlElement(name = "execution", type = WsTestExecution.class)
	})
	private List<T> items;
	
	public WsListResult() {
		super();
	}
	
	public WsListResult(Collection<T> items) {
		super();
		
		this.items = new ArrayList<T>();
		this.items.addAll(items);
		
	}

	public List<T> getItems() {
		items = ListUtils.emptyListIfNull(items);
		return items;
	}
	
	@Override
	public Iterator<T> iterator() {
		return getItems().iterator();
	}
	
	@Override
	public String toString() {
		return getClass().getSimpleName() + "[" + getItems() + "]";
	}
	
	public void clear() {
		getItems().clear();
	}
	
	public void add(T item) {
		getItems().add(item);
	}

}
